#-*- coding:UTF-8 -*-

"""
将srcFolder文件夹中所有图片归一化到48x48,存储至dstFolder文件夹中。
"""
import cv2
import os

def roiRegion(img):
    shape = img.shape
    # shape[0] is height, shape[1] is width
    if shape[0] == shape[1]:
        return 0, shape[0], 0, shape[1]
    elif shape[0] > shape[1]:
        return shape[0] - shape[1], shape[1], 0, shape[1]
    else:
        return 0, shape[0], 0, shape[0]
    
def imageResize(srcFolder, dstFolder):
    imgNames = os.listdir(srcFolder)
    process = 0
    for name in imgNames:
        if name.find('png') == -1: continue
        srcPath = srcFolder + '\\' + name
        img = cv2.imread(srcPath)
        (top, h, left, w) = roiRegion(img)
        imgSrc = img[top:top+h, left:left+w]
        imgDst = cv2.resize(imgSrc, (48, 48))
        dstPath = dstFolder + '\\' + name
        cv2.imwrite(dstPath, imgDst)
        process += 1
        print 'left: ', len(imgNames) - process
        
        
        
        
